﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using SBS.Common;
using System.Web;
using System.Xml;
using System.Xml.XPath;
using System.IO;
using System.Linq;

namespace SBS.Common
{
   /// <summary>
    /// C# Collection 基础类库扩展
   /// </summary>   
    public static class CollectionExtension
    {
        /// <summary>
        /// 按键排序。(该扩展位于 SBS.Common.dll 中)
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="Dict"></param>
        /// <param name="Comparison"></param>
        /// <returns></returns>
        public static Dictionary<TKey, TValue> OrderByKey<TKey, TValue>(this Dictionary<TKey, TValue> Dict)
        {
            return OrderByKey<TKey, TValue>(Dict, null);
        }
        /// <summary>
        /// 按键排序。(该扩展位于 SBS.Common.dll 中)
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="Dict"></param>
        /// <param name="Comparison"></param>
        /// <returns></returns>
        public static Dictionary<TKey, TValue> OrderByKey<TKey, TValue>(this Dictionary<TKey, TValue> Dict, Func<TKey, TKey, int> Comparison)
        {
            int Len = Dict.Count;
            List<TKey> Arr = new List<TKey>(Len);
            foreach (TKey item in Dict.Keys)
            {
                Arr.Add(item);
            }
            if (Comparison == null)
            {
                Arr.Sort();
            }
            else
            {                
                Arr.Sort(new Comparison<TKey>(Comparison));
            }
            return Arr.ToDictionary(m=>m,m=>Dict[m]);
            //Dictionary<TKey, TValue> Result = new Dictionary<TKey, TValue>(Len);
            //foreach (TKey item in Arr)
            //{
            //    Result.Add(item, Dict[item]);
            //}
            //return Result;
        }
        /// <summary>
        /// 按键排倒序。(该扩展位于 SBS.Common.dll 中)
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="Dict"></param>
        /// <param name="Comparison"></param>
        /// <returns></returns>
        public static Dictionary<TKey, TValue> OrderByKeyDescending<TKey, TValue>(this Dictionary<TKey, TValue> Dict)
        {
            return (from entry in Dict orderby entry.Key descending select entry).ToDictionary(pair => pair.Key, pair => pair.Value);
            //Dictionary<TKey, TValue> Result = new Dictionary<TKey, TValue>(Len);
            //foreach (TKey item in Arr)
            //{
            //    Result.Add(item, Dict[item]);
            //}
            //return Result;
        }
        /// <summary>
        /// 按键排序。(该扩展位于 SBS.Common.dll 中)
        /// </summary>
        /// <typeparam name="TKey"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="Dict"></param>
        /// <param name="Comparison"></param>
        /// <returns></returns>
        public static Dictionary<TKey, TValue> OrderByValue<TKey, TValue>(this Dictionary<TKey, TValue> Dict)
        {
            return (from entry in Dict orderby entry.Value ascending select entry).ToDictionary(pair => pair.Key, pair => pair.Value);

            //int Len = Dict.Count;
            //List<TKey> Arr = new List<TKey>(Len);
            //foreach (TKey item in Dict.Keys)
            //{
            //    Arr.Add(item);
            //}
            //Comparison<TKey> cc = new Comparison<TKey>(Comparison);
            //Arr.Sort(cc);
            //Dictionary<TKey, TValue> Result = new Dictionary<TKey, TValue>(Len);
            //foreach (TKey item in Arr)
            //{
            //    Result.Add(item, Dict[item]);
            //}
            //return Result;
        }

        /// <summary>
        /// 循环遍历集合，每个元素都传入委托中执行。该扩展方法位于 SBS.Common.dll 中。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Source"></param>
        /// <param name="Method"></param>
        public static void Each<T>(this IEnumerable<T> Source, Action<T> Method)
        {
            foreach (T Obj in Source)
            {
                Method(Obj);
            }
        }

        /// <summary>
        /// 按指定的选择器，执行 Method 委托。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Source"></param>
        /// <param name="Selector">选择器。如果选择器返回 true， 则执行 Method 委托。</param>
        /// <param name="Method"></param>
        public static void If<T>(this IEnumerable<T> Source, Func<T, bool> Selector, Action<T> Method)
        {
            foreach (T Obj in Source)
            {
                if (Selector(Obj))
                {
                    Method(Obj);
                }
            }
        }

    }
   
}
